﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameName1
{
    class Level : IWeaponObserver, IAngryAlienObserver, IProjectileObserver
    {
        public int Points { get; set; }
        public Level()
        {
            Aliens = new List<XnaAngryAlienDecorator>();
            Projectiles = new List<XnaProjectileDecorator>();

            SpaceShip spaceShip = new SpaceShip(new Vector2(400, 550));
            IWeapon weapon = new RocketLauncher();
            weapon.RegisterObserver(this);
            spaceShip.Weapon = weapon;

            SpaceShip = new XnaSpaceShipDecorator(spaceShip);
                       
            for (int i = 0; i < 55; i++)
            {
                AngryAlien angryAlien = new AngryAlien(new Vector2(50 + (i % 11) * 45, 50 + (i / 11) * 45));
                angryAlien.RegisterObserver(this);
                Aliens.Add(new XnaAngryAlienDecorator(angryAlien));
            }

        }
        
        public List<XnaAngryAlienDecorator> Aliens { get; set; }
        public List<XnaProjectileDecorator> Projectiles { get; set; }
        public XnaSpaceShipDecorator SpaceShip { get; set; }

        public void CheckColission()
        {
            List<XnaGameObjectDecorator> objects = ConcatenateLists();
            for (int i = 0; i < objects.Count; i++)
            {
                RemoveProjectiles();

                for (int j = i+1; j < objects.Count; j++)
                {
                    if (Intersects(objects[i].getColissionBox(), objects[j].getColissionBox()))
                    {
                        objects[i].HandleHit();
                        objects[j].HandleHit();
                    }
                }
            }
        }

        private List<XnaGameObjectDecorator> ConcatenateLists()
        {
            List<XnaGameObjectDecorator> list = Aliens.Cast<XnaGameObjectDecorator>().ToList();
            list.AddRange(Projectiles.Cast<XnaGameObjectDecorator>());
            list.Add(SpaceShip);

            return list;
        }
        private bool Intersects(Rectangle rectA, Rectangle rectB)
        {
            
            return (rectA.Right > rectB.Left && rectA.Left < rectB.Right &&
                    rectA.Bottom > rectB.Top && rectA.Top < rectB.Bottom);
        }

        private void RemoveProjectiles()
        {
            var boundaries = new Rectangle(0,0,800,600);
            for (int i = 0; i < Projectiles.Count; i++)
            {
                if (!Intersects(boundaries, Projectiles[i].getColissionBox()))
                {
                    Projectiles.RemoveAt(i);
                }
            }
        }
        
        public void Update(XnaProjectileDecorator projectile)
        {
            Projectiles.Add(projectile);
            projectile.RegisterObserver(this);
        }
        public void Update(AngryAlien angryAlien)
        {
            foreach (var xnaAngryAlienDecorator in Aliens)
            {
                if (xnaAngryAlienDecorator.AngryAlien == angryAlien)
                {
                    Aliens.Remove(xnaAngryAlienDecorator);
                    break;
                }
            }
        }
        public void Update(Projectile projectile)
        {
            foreach (var xnaProjectileDecorator in Projectiles)
            {
                if (xnaProjectileDecorator.Projectile == projectile)
                {
                    Projectiles.Remove(xnaProjectileDecorator);
                    break;
                }
            }
        }
    }
}
